const os = uni.getSystemInfoSync().osName;
console.log(os); // android 或 ios

const androidPermissions = {
  camera: "android.permission.CAMERA", // 摄像头权限
  location: "android.permission.ACCESS_FINE_LOCATION", // 位置权限
  calendar: {
    read: "android.permission.READ_CALENDAR", // // 日历读取权限
    write: "android.permission.WRITE_CALENDAR", // // 日历写入权限
  },
  storage: {
    read: "android.permission.READ_EXTERNAL_STORAGE", // 存储读取权限
    write: "android.permission.WRITE_EXTERNAL_STORAGE", // 存储写入权限
  },
  record: "android.permission.RECORD_AUDIO", // 麦克风权限
  contact: {
    read: "android.permission.READ_CONTACTS", // 联系人读取权限
    write: "android.permission.WRITE_CONTACTS", // 联系人写入权限
  },
  sms: {
    read: "android.permission.READ_SMS", // 短信读取权限
    send: "android.permission.SEND_SMS", //短信发送权限
    receive: "android.permission.RECEIVE_SMS", // 短信接收权限
  },
  state: "android.permission.READ_PHONE_STATE", // 手机识别码权限
  phone: "android.permission.CALL_PHONE", // 拨打电话权限
  log: "android.permission.READ_CALL_LOG", // 通话记录权限
};

function getPermissionList(permissionID) {
  const id = androidPermissions[permissionID];
  const list = [];
  if (typeof id == "string") {
    list[0] = id;
  } else {
    for (let key in id) {
      list.push(id[key]);
    }
  }
  return list;
}
// android检测方法
function androidPermissionCheck(permissionID) {
  return new Promise((resolve, reject) => {
    if (plus) {
      let ids = getPermissionList(permissionID);
      plus.android.requestPermissions(
        ids,
        function (result) {
          let res = 0;
          for (let i = 0; i < result.granted.length; i++) {
            let permission = result.granted[i];
            console.log("已获取的权限：", permission);
            res = 1;
          }
          for (let i = 0; i < result.deniedPresent.length; i++) {
            let permission = result.deniedPresent[i];
            console.log("本次已拒绝的权限：", permission);
            res = 0;
          }
          for (let i = 0; i < result.deniedAlways.length; i++) {
            let permission = result.deniedAlways[i];
            console.log("永久拒绝的权限：", permission);
            res = -1;
          }
          resolve({
            code: 1,
            data: res,
          });
        },
        (error) => {
          reject({
            code: 2,
            data: `code:${error.code},msg:${error.message}!`,
          });
        }
      );
    }
  });
}

// ios权限检测方法
function iosPermissionCheck(permissionID) {
  return new Promise((resolve, reject) => {
    let res = iosPermissions[permissionID]();
    resolve({
      code: 1,
      data: res,
    });
  });
}

// ios权限列表
const iosPermissions = {
  camera: iosCamera, // 摄像头
  location: iosLocation, // 位置
  calendar: iosCalendar, // 日历
  storage: iosAlbum, // 相册
  record: iosRecord, // 麦克风
  contact: iosContact, // 通讯录
  memo: iosMemo, // 备忘录
  push: iosPush, // 推送
};

function iosCamera() {
  if (plus) {
    let result = 0,
      avCaptureDevice = plus.ios.importClass("AVCaptureDevice"),
      authStatus = avCaptureDevice.authorizationStatusForMediaType("vide");
    if (authStatus === 3) {
      result = 1;
    } else {
      result = 0;
    }
    plus.ios.deleteObject(avCaptureDevice);
    return result;
  } else {
    return null;
  }
}

function iosLocation() {
  if (plus) {
    let result = 0,
      cLLocationManager = plus.ios.importClass("CLLocationManager"),
      authStatus = cLLocationManager.authorizationStatus(),
      enable = cLLocationManager.locationServicesEnabled();
    if (enable && authStatus != 2) {
      result = 1;
    } else {
      result = 0;
    }
    plus.ios.deleteObject(cLLocationManager);
    return result;
  } else {
    return null;
  }
}

function iosCalendar() {
  if (plus) {
    let result = 0,
      eKEventStore = plus.ios.importClass("EKEventStore"),
      authStatus = eKEventStore.authorizationStatusForEntityType(0);
    if (authStatus === 3) {
      result = 1;
    } else {
      result = 0;
    }
    plus.ios.deleteObject(eKEventStore);
    return result;
  } else {
    return null;
  }
}

function iosAlbum() {
  if (plus) {
    let result = 0,
      photoLibrary = plus.ios.importClass("PhotoLibrary"),
      authStatus = photoLibrary.authorizationStatus();
    if (authStatus === 3) {
      result = 1;
    } else {
      result = 0;
    }
    plus.ios.deleteObject(photoLibrary);
    return result;
  } else {
    return null;
  }
}

function iosRecord() {
  if (plus) {
    let result = 0,
      aVAudioSession = plus.ios.importClass("AVAudioSession"),
      aVAudio = aVAudioSession.sharedInstance(),
      authStatus = aVAudio.recordPermission();
    if ([1684369017, 1970168948].includes(authStatus)) {
      result = 0;
    } else {
      result = 1;
    }
    plus.ios.deleteObject(aVAudioSession);
    return result;
  } else {
    return null;
  }
}

function iosContact() {
  if (plus) {
    let result = 0,
      contactStore = plus.ios.importClass("CNContactStore"),
      authStatus = contactStore.authorizationStatusForEntityType(0);
    if (authStatus === 3) {
      result = 1;
    } else {
      result = 0;
    }
    plus.ios.deleteObject(contactStore);
    return result;
  } else {
    return null;
  }
}

function iosMemo() {
  if (plus) {
    let result = 0,
      eKEventStore = plus.ios.importClass("EKEventStore"),
      authStatus = eKEventStore.authorizationStatusForEntityType(1);
    if (authStatus === 3) {
      result = 1;
    } else {
      result = 0;
    }
    plus.ios.deleteObject(eKEventStore);
    return result;
  } else {
    return null;
  }
}

function iosPush() {
  if (plus) {
    let result = 0,
      application = plus.ios.importClass("UIApplication"),
      app = application.sharedApplication(),
      authStatus = 0;
    if (app.currentUserNotificationSettings) {
      let setting = app.currentUserNotificationSettings(),
        authStatus = setting.plusGetAttribute("types");
      if (authStatus === 0) {
        result = 0;
      } else {
        result = 1;
      }
    } else {
      authStatus = app.enabledRemoteNotificationTypes();
      if (authStatus === 0) {
        result = 0;
      } else {
        result = 1;
      }
    }
    plus.ios.deleteObject(eKEventStore);
    return result;
  } else {
    return null;
  }
}

const permissionCheck = {
  android: androidPermissionCheck,
  ios: iosPermissionCheck,
};
 
const permission = permissionCheck[os];
 
export default permission;